home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Miscellaneous / gdbm / update.c < prev    next >
Text File  |  1992-04-05  |  3KB  |  117 lines

  1. /* update.c - The routines for updating the file to a consistent state. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990, 1991  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 1, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@cs.wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.         phone:  (206) 676-3035
  27.        
  28. *************************************************************************/
  29.  
  30.  
  31. #include "gdbmdefs.h"
  32.  
  33.  
  34. /* This procedure writes the header back to the file described by DBF. */
  35.  
  36. static 
  37. write_header (dbf)
  38.      gdbm_file_info *dbf;
  39. {
  40.   int  num_bytes;    /* Return value for write. */
  41.   long file_pos;    /* Return value for lseek. */
  42.  
  43.   file_pos = lseek (dbf->desc, 0, L_SET);
  44.   if (file_pos != 0) _gdbm_fatal (dbf, "lseek error");
  45.   num_bytes = write (dbf->desc, (char *) dbf->header, dbf->header->block_size);
  46.   if (num_bytes != dbf->header->block_size)
  47.     _gdbm_fatal (dbf, "write error");
  48.  
  49.   /* Wait for all output to be done. */
  50.   fsync (dbf->desc);
  51. }
  52.  
  53.  
  54. /* After all changes have been made in memory, we now write them
  55.    all to disk. */
  56. _gdbm_end_update (dbf)
  57.      gdbm_file_info *dbf;
  58. {
  59.   int  num_bytes;    /* Return value for write. */
  60.   long file_pos;    /* Return value for lseek. */
  61.   
  62.   
  63.   /* Write the current bucket. */
  64.   if (dbf->bucket_changed)
  65.     {
  66.       _gdbm_write_bucket (dbf, dbf->cache_entry);
  67.       dbf->bucket_changed = FALSE;
  68.     }
  69.  
  70.   /* Write the other changed buckets if there are any. */
  71.   if (dbf->second_changed)
  72.     {
  73.       int index;
  74.  
  75.       for (index = 0; index < CACHE_SIZE; index++)
  76.     if (dbf->bucket_cache[index].ca_changed)
  77.       {
  78.         _gdbm_write_bucket (dbf, &dbf->bucket_cache[index]);
  79.       }
  80.       dbf->second_changed = FALSE;
  81.     }
  82.   
  83.   /* Write the directory. */
  84.   if (dbf->directory_changed)
  85.     {
  86.       file_pos = lseek (dbf->desc, dbf->header->dir, L_SET);
  87.       if (file_pos != dbf->header->dir) _gdbm_fatal (dbf, "lseek error");
  88.       num_bytes = write (dbf->desc, (char *) dbf->dir, dbf->header->dir_size);
  89.       if (num_bytes != dbf->header->dir_size)
  90.     _gdbm_fatal (dbf, "write error");
  91.       dbf->directory_changed = FALSE;
  92.       if (!dbf->header_changed) fsync (dbf->desc);
  93.     }
  94.  
  95.   /* Final write of the header. */
  96.   if (dbf->header_changed)
  97.     {
  98.       write_header (dbf);
  99.       dbf->header_changed = FALSE;
  100.     }
  101. }
  102.  
  103.  
  104. /* If a fatal error is detected, come here and exit. VAL tells which fatal
  105.    error occured. */
  106. _gdbm_fatal (dbf, val)
  107.      gdbm_file_info *dbf;
  108.      char *val;
  109. {
  110.   if (dbf->fatal_err != NULL)
  111.     (*dbf->fatal_err) (val);
  112.   else
  113.     fprintf (stderr, "gdbm fatal: %s.\n", val);
  114.   exit (-1);
  115. }
  116.  
  117.